unit DiscuzAPI;

interface

uses
  System.Classes,
  idHTTP;

type
{$M+}
  TAuthData = class
  Private Type
    TVariables = Class
    private type
      TNotice = class
      Public
        NewPush, NewPM, NewPrompt, NewMyPost: integer;
      end;

      TAllowPerm = class
      Private Type
        TAllowUpload = Class
        Public
          jpg, jpeg, gif, png, mp3, txt, zip, rar, pdf: integer;
        End;

        TAttachreMain = Class
        Public
          size, count: integer;
        End;
      public
        AllowReply, AllowPost: integer;
        UploadHash: String;
        AllowUpload: TAllowUpload;
        AttachreMain: TAttachreMain;
        Constructor Create;
        Destructor Destroy;
      end;
    public
      CookiePre, Auth, SaltKey, Member_Username, FormHash, IsModerator: String;
      Member_uId, GroupId, ReadAccess: string;
      Notice: TNotice;
      AllowPerm: TAllowPerm;
      Constructor Create;
      Destructor Destroy;
    End;

    TMessage = Class
    public
      MessageVal, MessageStr: String;
    End;
  Public
    Version: String;
    Charset: String;
    Variables: TVariables;
    Messages: TMessage;
    Constructor Create;
    Destructor Destroy;
  end;

  TOnMessage = procedure(Sender: TObject; MsgVal, MsgString: String) of object;

  TDiscuzAPI = class
  private
    fHTTP: TIdHTTP;
    fURL: String;
    fOnChangeSource: TNotifyEvent;
    fSource: String;
    fAuthData: TAuthData;
    fOnMessage: TOnMessage;
  protected
    procedure ParseAuth(Source: String);
  public
    Function Login(User, Password: String): boolean;
    Constructor Create;
    Destructor Destroy; override;
  published
    property AuthData: TAuthData read fAuthData write fAuthData;
    property OnChangeSource: TNotifyEvent read fOnChangeSource
      write fOnChangeSource;
    Property Source: String read fSource write fSource;
    property URL: String read fURL write fURL;
    property OnMessage: TOnMessage read fOnMessage write fOnMessage;
  end;

implementation

uses
  System.Variants,
  uLkJSON,
  System.SysUtils,
  Discuz.Constants;
{ TDiscuzAPI }

constructor TDiscuzAPI.Create;
begin
  fHTTP := TIdHTTP.Create;
  fAuthData := TAuthData.Create;
  fURL := TDiscuzConst.URL_DEFAULT;
end;

destructor TDiscuzAPI.Destroy;
begin
  fHTTP.Free;
  inherited Destroy;
end;

function TDiscuzAPI.Login(User, Password: String): boolean;
var
  Params: TStringList;
begin
  Params := TStringList.Create;
  try
    Params.Add('password=' + Password);
    Params.Add('username=' + User);
    fSource := fHTTP.Post(URL + TDiscuzConst.URL_Register, Params);
    ParseAuth(fSource);
    if Assigned(fOnChangeSource) then
      fOnChangeSource(Self);
  finally
    Params.Free;
  end;
  Result := true;
end;

procedure TDiscuzAPI.ParseAuth(Source: String);
var
  JSON, xs: TlkJSONobject;
  ws: TlkJSONstring;
var
  tmp1, tmp2: String;
begin
  // try
  // raise Exception.Create(Source);

  JSON := TlkJSON.ParseText(Source) as TlkJSONobject;

  ws := JSON.Field['Version'] as TlkJSONstring;
  fAuthData.Version := ws.Value;

  ws := JSON.Field['Charset'] as TlkJSONstring;
  fAuthData.Charset := ws.Value;

  xs := JSON.Field['Variables'] as TlkJSONobject;

  ws := xs.Field['cookiepre'] as TlkJSONstring;
  fAuthData.Variables.CookiePre := ws.Value;
  if xs.Field['auth'] is TlkJSONstring then
  begin
    ws := xs.Field['auth'] as TlkJSONstring;
    fAuthData.Variables.Auth := ws.Value;
  end;

  ws := xs.Field['saltkey'] as TlkJSONstring;
  fAuthData.Variables.SaltKey := ws.Value;

  ws := xs.Field['member_uid'] as TlkJSONstring;
  fAuthData.Variables.Member_uId := ws.Value;

  ws := xs.Field['member_username'] as TlkJSONstring;
  fAuthData.Variables.Member_Username := ws.Value;

  ws := xs.Field['groupid'] as TlkJSONstring;
  fAuthData.Variables.GroupId := ws.Value;

  ws := xs.Field['formhash'] as TlkJSONstring;
  fAuthData.Variables.FormHash := ws.Value;

  fAuthData.Variables.IsModerator := (xs.Field['ismoderator'] as TlkJSONnull)
    .SelfTypeName;

  ws := xs.Field['readaccess'] as TlkJSONstring;
  fAuthData.Variables.ReadAccess := ws.Value;

  ws := xs.Field['notice'].Field['newpush'] as TlkJSONstring;
  fAuthData.Variables.Notice.NewPush := ws.Value;

  ws := xs.Field['notice'].Field['newpush'] as TlkJSONstring;
  fAuthData.Variables.Notice.NewPush := ws.Value;

  ws := xs.Field['notice'].Field['newpm'] as TlkJSONstring;
  fAuthData.Variables.Notice.NewPM := ws.Value;

  ws := xs.Field['notice'].Field['newprompt'] as TlkJSONstring;
  fAuthData.Variables.Notice.NewPrompt := ws.Value;

  ws := xs.Field['notice'].Field['newmypost'] as TlkJSONstring;
  fAuthData.Variables.Notice.NewMyPost := ws.Value;

  ws := xs.Field['allowperm'].Field['allowpost'] as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowPost := ws.Value;
  ws := xs.Field['allowperm'].Field['allowreply'] as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowReply := ws.Value;
  ws := xs.Field['allowperm'].Field['allowupload'].Field['jpg']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowUpload.jpg := ws.Value;
  ws := xs.Field['allowperm'].Field['allowupload'].Field['jpeg']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowUpload.jpeg := ws.Value;
  ws := xs.Field['allowperm'].Field['allowupload'].Field['gif']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowUpload.gif := ws.Value;
  ws := xs.Field['allowperm'].Field['allowupload'].Field['png']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowUpload.png := ws.Value;

  ws := xs.Field['allowperm'].Field['allowupload'].Field['mp3']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowUpload.mp3 := ws.Value;

  ws := xs.Field['allowperm'].Field['allowupload'].Field['txt']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowUpload.txt := ws.Value;

  ws := xs.Field['allowperm'].Field['allowupload'].Field['zip']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowUpload.zip := ws.Value;

  ws := xs.Field['allowperm'].Field['allowupload'].Field['rar']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowUpload.rar := ws.Value;

  ws := xs.Field['allowperm'].Field['allowupload'].Field['pdf']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowUpload.pdf := ws.Value;

  // ----------------------

  ws := xs.Field['allowperm'].Field['allowupload'].Field['pdf']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AllowUpload.pdf := ws.Value;

  ws := xs.Field['allowperm'].Field['attachremain'].Field['size']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AttachreMain.size := ws.Value;
  ws := xs.Field['allowperm'].Field['attachremain'].Field['count']
    as TlkJSONstring;
  fAuthData.Variables.AllowPerm.AttachreMain.count := ws.Value;

  ws := xs.Field['allowperm'].Field['uploadhash'] as TlkJSONstring;
  fAuthData.Variables.AllowPerm.UploadHash := ws.Value;

  ws := JSON.Field['Message'].Field['messageval'] as TlkJSONstring;
  fAuthData.Messages.MessageVal := ws.Value;

  ws := JSON.Field['Message'].Field['messagestr'] as TlkJSONstring;
  fAuthData.Messages.MessageStr := (ws.Value);
  if Assigned(OnMessage) then
    with fAuthData.Messages do
      OnMessage(Self, MessageVal, MessageStr);

  // JSON.

  // except
  // on E: Exception do
  // raise Exception.Create(E.ClassName + #13#10 + E.Message);
  // end;

end;

{ TAuthData }

constructor TAuthData.Create;
begin
  // inherited Create;
  Variables := TVariables.Create;
  Messages := TMessage.Create;
end;

destructor TAuthData.Destroy;
begin
  Variables.Free;
  // inherited Destroy;
end;

{ TAuthData.TVariables }

constructor TAuthData.TVariables.Create;
begin
  Notice := TNotice.Create;
  AllowPerm := TAllowPerm.Create;
end;

destructor TAuthData.TVariables.Destroy;
begin
  Notice.Free;
  AllowPerm.Free;
end;

{ TAuthData.TVariables.TAllowPerm }

constructor TAuthData.TVariables.TAllowPerm.Create;
begin
  AllowUpload := TAllowUpload.Create;
  AttachreMain := TAttachreMain.Create;
end;

destructor TAuthData.TVariables.TAllowPerm.Destroy;
begin
  AllowUpload.Free;
  AttachreMain.Free;
end;

end.
